package cn.tworice.client.service;

import cn.tworice.auth.util.LoginUserUtil;
import cn.tworice.common.util.excel.InductsListenerService;
import cn.tworice.api.CrudService;
import cn.tworice.client.dao.CurrencyTransactionDao;
import cn.tworice.common.entity.code.*;
import cn.tworice.vo.SourceItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CurrencyTransactionService implements InductsListenerService<CurrencyTransaction>, CrudService<CurrencyTransaction> {
    @Autowired
    private CurrencyTransactionDao dao;

    @Override
    public List<CurrencyTransaction> list(Integer page, Integer pageSize, CurrencyTransaction entity) {
        return dao.queryList(page * pageSize, pageSize, entity);
    }

    @Override
    public int total(CurrencyTransaction entity) {
        return dao.queryTotal(entity);
    }

    @Override
    public int set(CurrencyTransaction entity) {
        entity.setUpdateTime(System.currentTimeMillis());
        if (entity.getId() == null) {
            entity.setCreateTime(System.currentTimeMillis());
            return dao.insert(entity);
        } else {
            return dao.update(entity);
        }
    }

    @Override
    public boolean update(CurrencyTransaction entity) {
        return dao.update(entity) == 1;
    }

    @Override
    public int del(Integer[] ids) {
        return dao.del(ids);
    }

    @Override
    public int save(List<CurrencyTransaction> list) {
        list.forEach(item -> {
            item.setCreateTime(System.currentTimeMillis());
            item.setUpdateTime(System.currentTimeMillis());
        });
        return dao.insertList(list);
    }

    @Override
    public CurrencyTransaction info(int id) {
        return dao.queryInfo(id);
    }

    public Double amount(String adminId) {
        Double aDouble = dao.querySumByCreator(adminId);
        Double aDouble1 = dao.querySumBySource(adminId);
        return aDouble1==null?(aDouble==null?0:aDouble):aDouble-aDouble1;
    }

    /**
     * 计算来源方给接收方转了多少钱
     * @param receive 接收方
     * @param source 来源方
     * @return java.lang.Double
     * @author 二饭 [2023/3/27]
     **/
    public Double direction(String receive,String source){
        if(source==null){
            return this.amount(receive);
        }
        Double a = dao.direction(receive, source);
        return a==null?0:a;
    }

    public SourceItem source(String adminId) {
        return getSource(adminId,null, new ArrayList<>());
    }

    /**
     * TODO 未测
     * @param adminId 接收方
     * @param exclude 排除
     * @return cn.tworice.vo.SourceItem
     * @author 二饭 [2023/3/26]
     **/
    private SourceItem getSource(String adminId,String source,List<String> exclude){
        SourceItem sourceItem = new SourceItem();
        sourceItem.setValue(this.direction(adminId,source));
        if(source==null){
            sourceItem.setName(adminId);
            source=adminId;
        }else{
            sourceItem.setName(source);
        }

        sourceItem.setChildren(new ArrayList<>());
        // 设置 谁给当前账户转账 查询条件
        CurrencyTransaction currencyTransaction = new CurrencyTransaction();
        currencyTransaction.setCreator(source);
        currencyTransaction.setType("转账");
        List<CurrencyTransaction> list = dao.list( currencyTransaction);

        for(CurrencyTransaction item:list){
            if(!exclude.contains(item.getSource())){
                exclude.add(item.getSource());
                sourceItem.getChildren().add(this.getSource(item.getCreator(), item.getSource(), exclude));
            }
        }
        return sourceItem;
    }
}